<?php

Class Index extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->data['controller'] = 'index';
        $this->load->model('Mindex');
        $this->load->model('Mother');
    }

    private function current_url() {
        $CI = & get_instance();

        $url = $CI->config->site_url($CI->uri->uri_string());
        return $_SERVER['QUERY_STRING'] ? $url . '?' . $_SERVER['QUERY_STRING'] : $url;
    }

    public function index() {
        if ($this->getParamString('res') != null && $this->getParamInt('preview_user') <= 0) {
            // Lưu param vào session
            $this->session->set_userdata(array(
                'res' => $this->getParamString('res'),
                'chal' => $this->getParamString('challenge') ? $this->getParamString('challenge') : $this->getParamString('chal'),
                'uamip' => $this->getParamString('uamip'),
                'uamport' => $this->getParamString('uamport'),
                'userurl' => $this->getParamString('userurl'),
                'mac' => $this->getParamString('mac'),
                'nasid' => $this->getParamString('nasid'))
            );
        }

        /**
         * 
         * @void Param Authentication
         * 
         */
        if (!in_array($this->session->userdata('modem_user'), array('', null))) {
            $modem_user = $this->session->userdata('modem_user');
        } else {
            $modem_user = 'bob';
        }
        if (!in_array($this->session->userdata('modem_password'), array('', null))) {
            $modem_password = $this->session->userdata('modem_password');
        } else {
            $modem_password = 'hello';
        }

        $param = (object) array(
                    'uamsecret' => 'testing123',
                    'res' => $this->getParamString('res') ? $this->getParamString('res') : $this->session->userdata('res'),
                    'mac' => $this->getParamString('mac') ? $this->getParamString('mac') : $this->session->userdata('mac'),
                    'nasid' => $this->getParamString('nasid') ? $this->getParamString('nasid') : $this->session->userdata('nasid'),
                    'challenge' => $this->getParamString('challenge') ? $this->getParamString('challenge') : $this->session->userdata('chal'),
                    'uamip' => $this->getParamString('uamip') ? $this->getParamString('uamip') : $this->session->userdata('uamip'),
                    'uamport' => $this->getParamString('uamport') ? $this->getParamString('uamport') : $this->session->userdata('uamport'),
                    'userurl' => $this->getParamString('userurl') ? $this->getParamString('userurl') : $this->session->userdata('userurl'),
                    'username_slow' => $modem_user,
                    'password_slow' => $modem_password,
                    'username_normal' => $modem_user,
                    'password_normal' => $modem_password,
                    'username_fast' => $modem_user,
                    'password_fast' => $modem_password
        );

        //Param code của facebook khi đăng nhập trả về
        $code = $this->getParamString('code');

        //Đổ last action lên view
        $this->data['lastaction'] = $this->session->userdata('lastaction');

        $nasid = $param->nasid;

        if ($nasid == "") {
            $id_place = 0;
        } else {
            $id_place = $this->Mindex->getIdPlace($nasid);
        }

        $this->data['id_place'] = $id_place;

        $listCampaign = '';

        if ($id_place == 0 && $code == '') {

            if ($this->getParamInt('preview_user') > 0) {
                /*
                 * @Trang xem trên thiết bị giả lập tại admin
                 */
                $listCampaign = $this->Mindex->getCampaignPreview($this->getParamInt('preview_user'));
                $this->data['data'] = $listCampaign;
            } else {
                /*
                 * @Trang báo lỗi khi không tồn tại địa chỉ id_place 
                 */
                $listCampaign = $this->Mindex->getCampaignError(16);
                $this->data['data'] = $listCampaign;
            }


            /*
             * @Sử dụng layout 
             */
            $this->data['action'] = __FUNCTION__;
            $this->load->view('temp/layout', $this->data);
        } else if ($param->res == 'notyet' && $id_place > 0) {//Điều kiện ở trang chào và id_place hợp lệ
            /*
             * @Gán params nasid, mac, authentication_link lên view 
             */
            $this->data['nasid'] = $param->nasid;
            $this->data['mac'] = $param->mac;
            $this->data['authentication_link'] = $this->renderLink($param);

            /*
             * @Lấy Hệ điều hành thiết bị lên view
             */
            $this->data['name_devide'] = $this->devide_name();

            /*
             * @Khởi tạo chiến dịch 
             * @Nếu danh sách không có chiến dịch sẽ gọi chiến dịch mặc định type_adv = 15
             */
            $list = $this->Mindex->getCampaigns($id_place);
            //print_r($list);
            if (count($list) == 0) {
                $this->data['data'] = $this->Mindex->getCampaignError(15);
            } else {
                $this->data['data'] = $list;
                /*
                 * @Nếu chiến dịch có share facebook thì check đăng nhập
                 * @Gán lên view
                 */
                if ($this->hasShareFb($list)) {
                    $this->data['codefb'] = $code == '' ? 0 : $code;
                    $this->data['loginFB'] = $this->facebook_check_login();
                    $this->session->set_userdata(array('site_url' => $this->current_url()));
                }
            }

            /*
             * @Sử dụng layout 
             */
            $this->data['action'] = __FUNCTION__;
            $this->load->view('temp/layout', $this->data);
        } else {
            if ($param->res == 'failed')
                $this->display_failed($param);
            if ($param->res == 'logoff')
                $this->display_logoff($param);
            if ($param->res == 'success')
                $this->display_success();
            if ($param->res == 'timeout')
                $this->display_logoff($param);
            if ($param->res == 'already')
                $this->display_already($param);
        }
    }

    private function hasShareFb($listCampaign) {
        foreach ($listCampaign as $item) {
            if ($item->type_ads == 6 || $item->type_ads == 17) {
                return true;
            }
        }
        return false;
    }

    public function lastaction() {
        /*
         * @Lưu thông tin share facebook tới session 
         * @Thông tin này sẽ lại được đổ lên view khi facebook rediect về trang chào
         */
        if ($this->getParamString('id_campaign') == '' || $this->getParamString('action') == '') {
            die('201');
        }

        $lastaction = array('id_campaign' => $this->getParamInt('id_campaign'),
            'slide_index' => $this->getParamInt('slide_index'),
            'action' => $this->getParamString('action')
                //'data' => json_decode(strip_slashes($this->getParamString('data'))
        );
        $this->session->set_userdata(array(
            'lastaction' => json_encode($lastaction)
        ));
        die();
    }

    private function devide_name() {
        ini_set('mongo.native_long', 1);
        require_once(PROJECT_PATH . '/getinfo/index.php');
        $info = new UAParser;

        $ua = $_SERVER['HTTP_USER_AGENT'];
        $result = $info->parse($ua);
        @$exp_Os = explode(' ', $result->os->family);
        @$OsName = $exp_Os[0];
        $devide_info = array(
            "brower_name" => $result->ua->family,
            "brower_name_ver" => $result->ua->toString,
            "os_name_ver" => $result->os->toString,
            "devide_name" => $result->device->family,
            "devide_name_ver" => $result->device->family,
        );
        return $devide_info['devide_name'] == 'Other' ? $devide_info['os_name_ver'] : $devide_info['devide_name'];
    }

    private function facebook_check_login() {

        $checkLoginOld = $this->Mindex->getAccessTokenDB($this->session->userdata('mac'));
        if ($checkLoginOld['status'] == '200') {
            return $checkLoginOld;
        } else {

            $return_url = site_url(); //'?'.$url[1];
            require_once PROJECT_PATH . "/lib/fb/facebook.php";
            //,user_checkins,publish_checkins,user_tagged_places
            $fbPermissions = 'user_about_me,publish_actions';  //Required facebook permissions
            $facebook = new Facebook(array(
                'appId' => '563383537078971', //225056930986230
                'secret' => '15d6d14b8f57b05e73e76550c3bdc30a', //ecffbd143b8365f2e82fdb31dcea331c
            ));
            $user_fb = $facebook->getUser();
            $loginUrl = $facebook->getLoginUrl(array('redirect_uri' => $return_url, 'scope' => $fbPermissions));
            $profile = (object) array();

            if ($user_fb) {
                $token = $facebook->_getUserAccess();
                $user_profile = $facebook->api('/me');
                $profile->id = $user_profile['id'];
                $profile->name = $user_profile['name'];
                $profile->email = isset($user_profile['email']) ? $user_profile['email'] : $user_profile['link'];
                if ($token != null && $token != '') {
                    $this->session->set_userdata(array('token' => $token, 'fbid' => $user_fb));
                    $run = $this->Mindex->saveAccessTokenDB($user_fb, $token, $this->session->userdata('mac'), $profile->name, $profile->email);
                }
                return array('status' => '200', 'urlLogin' => null, 'user_profile' => $profile, 'ctmf' => 1);
            } else {
                return array('status' => '201', 'urlLogin' => $loginUrl, 'user_profile' => $profile, 'ctmf' => 1);
            }
        }
    }

    public function facebookshare() {
        //die(json_encode(array('status' => '200', 'result' => 'TEST')));

        require_once PROJECT_PATH . "/lib/fbauto/facebook.php";
        $token = $this->session->userdata('token');
        $fbid = $this->session->userdata('fbid');
        $config = array(
            'appId' => '563383537078971', //225056930986230
            'secret' => '15d6d14b8f57b05e73e76550c3bdc30a', //ecffbd143b8365f2e82fdb31dcea331c
            'tokennhatnv' => $token,
            'idnhatnv' => $fbid
        );
        $facebook = new Facebook($config);
        $param = (object) $this->getParamArray('param');
        try {
            $attachment = array(
                'message' => $param->msg,
                'link' => $param->link,
                'picture' => $param->pic,
                'description' => $param->desc,
                'name' => $param->name
            );
            $post_url = '/me/feed';
            $postResult = $facebook->api($post_url, 'post', $attachment);

            if (isset($postResult['id'])) {
                if ($postResult['id'] != null) {
                    $this->Mother->saveFacebookshareDB($this->getParamString('mac'), $this->getParamString('nasid'));
                    die(json_encode(array('status' => '200', 'result' => $postResult)));
                } else {
                    die(json_encode(array('status' => '201', 'result' => $postResult)));
                }
            }
        } catch (FacebookApiException $e) {
            die(json_encode(array('status' => '201', 'result' => $e)));
        }
    }

    public function facebooksharefanpage() {
        //die(json_encode(array('status' => '200', 'result' => 'TEST')));

        require_once PROJECT_PATH . "/lib/fbauto/facebook.php";
        $token = $this->session->userdata('token');
        $fbid = $this->session->userdata('fbid');
        $config = array(
            'appId' => '563383537078971', //225056930986230
            'secret' => '15d6d14b8f57b05e73e76550c3bdc30a', //ecffbd143b8365f2e82fdb31dcea331c
            'tokennhatnv' => $token,
            'idnhatnv' => $fbid
        );
        $facebook = new Facebook($config);

        $param = (object) $this->getParamArray('param');
        $photo = $this->getParamString('pic');

        if (!in_array($photo, array(null, '', '0'))) {
            $timenow = time() . rand(0, 9999);
            $nameimagess = 'share-photo-' . $timenow . '.png';
            $path = 'public/sharefp/';
            @mkdir(PROJECT_PATH . '/' . $path, 0301);
            $paths = PROJECT_PATH . '/' . $path;
            $arr_exp = array('[removed]', '[removed');
            $exp = str_replace($arr_exp, '', $photo);
            $this->savefile($exp, $paths, $nameimagess);
            $img1 = site_url() . $path . 'tmp/' . $nameimagess;
        } else {
            $img1 = '';
        }

        try {
            $attachment = array(
                'message' => $param->msg,
                'url' => $img1
            );

            $post_url = '/' . $param->idfanpage . '/photos';
            $postResult = $facebook->api($post_url, 'post', $attachment);

            if (isset($postResult['id'])) {
                if ($postResult['id'] != null) {
                    $this->Mother->saveFacebookshareDB($this->getParamString('mac'), $this->getParamString('nasid'));
                    @unlink(PROJECT_PATH . '/' . $path . 'tmp/' . $nameimagess);
                    die(json_encode(array('status' => '200', 'result' => $postResult)));
                } else {
                    @unlink(PROJECT_PATH . '/' . $path . 'tmp/' . $nameimagess);
                    die(json_encode(array('status' => '201', 'result' => $postResult)));
                }
            } else {
                die(json_encode(array('status' => '201', 'result' => $postResult)));
            }
        } catch (FacebookApiException $e) {
            @unlink(PROJECT_PATH . '/' . $path . 'tmp/' . $nameimagess);
            die(json_encode(array('status' => '201', 'result' => $e)));
        }
    }

    public function savefilephoto() {
        $photo = $this->getParamString('pic');
        if (!in_array($photo, array(null, '', '0'))) {

            $timenow = time() . rand(0, 9999);
            $nameimagess = 'share-photo-' . $timenow . '.png';
            $path = 'public/sharefp/';
            @mkdir(PROJECT_PATH . '/' . $path, 0301);
            $paths = PROJECT_PATH . '/' . $path;
            $arr_exp = array('[removed]', '[removed');
            $exp = str_replace($arr_exp, '', $photo);
            $this->savefile($exp, $paths, $nameimagess);
            $img1 = site_url() . $path . 'tmp/' . $nameimagess;
        } else {
            $img1 = '';
        }

        die($img1);
    }

    public function savefiletoclient() {
        $filename = $this->getParamString('filename');
        //die(basename($filename));
        $this->load->helper('download');
        $data = file_get_contents($filename); // Read the file's contents
        $name = 'myphoto.jpg';

        force_download($name, $data);
        $path = 'public/sharefp/';
        unlink(PROJECT_PATH . "/" . $path . "tmp/" . basename($filename));
    }

    public function facebookcheckin() {
        require_once PROJECT_PATH . "/lib/fbauto/facebook.php";
        $token = $this->session->userdata('token');
        $fbid = $this->session->userdata('fbid');
        $config = array(
            'appId' => '563383537078971', //225056930986230
            'secret' => '15d6d14b8f57b05e73e76550c3bdc30a', //ecffbd143b8365f2e82fdb31dcea331c
            'tokennhatnv' => $token,
            'idnhatnv' => $fbid
        );

        $facebook = new Facebook($config);
        $param = (object) $this->getParamArray('param');
        try {
            $attachment = array(
                'access_token' => $token,
                'message' => 'Cảm thấy tuyệt vời',
                'place' => '440695306094991',
            );

            $postResult = $facebook->api("/$fbid/feed", 'POST', $attachment);

            if (isset($postResult['id'])) {
                if ($postResult['id'] != null) {
                    die(json_encode(array('status' => '200', 'result' => $postResult)));
                } else {
                    die(json_encode(array('status' => '201', 'result' => $postResult)));
                }
            } else {
                die(json_encode(array('status' => '201', 'result' => $postResult)));
            }
        } catch (FacebookApiException $e) {
            die(json_encode(array('status' => '201', 'result' => $e)));
        }
    }

    public function cronactive() {
        echo $this->Mindex->activeAdv();
    }

    public function croncomplate() {
        echo $this->Mindex->complateAdv();
    }

    public function checklike() {
        echo $this->Mindex->checklikeDB();
    }

    public function insertlikefb() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $id_place = $this->getParamString('id_place');
        $facebook_url = $this->getParamString('facebook_url');
        $id_campaign = $this->getParamString('id_campaign');
        $fbid = $this->getParamString('fbid');
        $email = $this->getParamString('email');
        $fullname = $this->getParamString('fullname');
        $name_device = $this->getParamString('name_device');

        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        echo $this->Mindex->insertlikefbDB($mac, $nasid, $id_place, $facebook_url, $id_campaign, $fbid, $email, $fullname, $name_device);
    }

    public function getListJob() {
        die($this->Mindex->getListJob(''));
    }

    public function email() {
        $email = $this->getParamString('email');
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $name_device = $this->getParamString('name_device');
        $id_campaign = $this->getParamInt('id_campaign');
        $id_place = $this->getParamInt('id_place');

        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        die($this->Mother->saveEmailDB($mac, $nasid, $id_campaign, $name_device, $id_place, $email));
    }

    public function phone() {
        $mobile = $this->getParamString('phone');
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $name_device = $this->getParamString('name_device');
        $id_campaign = $this->getParamInt('id_campaign');
        $id_place = $this->getParamInt('id_place');

        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        die($this->Mother->saveMobileDB($mac, $nasid, $id_campaign, $name_device, $id_place, $mobile));
    }

    public function free() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $name_device = $this->getParamString('name_device');
        $id_campaign = $this->getParamInt('id_campaign');

        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        die($this->Mother->saveFreeDB($mac, $nasid, $id_campaign, $name_device));
    }

    public function campaigndefault() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $name_device = $this->getParamString('name_device');
        $id_campaign = $this->getParamInt('id_campaign');
        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        die($this->Mother->savecampaignDefaultDB($mac, $nasid, $id_campaign, $name_device));
    }

    public function visit() {
        $link = $this->getParamString('link');
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $name_device = $this->getParamString('name_device');
        $id_campaign = $this->getParamInt('id_campaign');

        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        die($this->Mother->saveVisitDb($mac, $nasid, $id_campaign, $name_device, $link));
    }

    public function video() {
        $link = $this->getParamString('video_link');
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $name_device = $this->getParamString('name_device');
        $id_campaign = $this->getParamInt('id_campaign');

        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        die($this->Mother->saveVideoDb($mac, $nasid, $id_campaign, $name_device, $link));
    }

    public function insertlogaccess() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $id_place = $this->getParamString('id_place');
        $id_campaign = $this->getParamString('id_campaign');
        $code = $this->getParamString('code');
        die($this->Mindex->insertLogAccess($mac, $nasid, $id_place, $id_campaign, $code));
    }

    public function survey() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $id_place = $this->getParamString('id_place');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $formvalue = $this->getParamString('formvalue');
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));

        die($this->Mother->saveSurveyDb($mac, $nasid, $id_campaign, $name_device, $id_place, $formvalue));
    }

    private function wifipassword_check($id_campaign, $client_password) {
        $data = $this->Mindex->getCampaign($id_campaign);
        if (count($data) == 1) {
            //Lấy password từ db
            $password = '';
            $data = json_decode(strip_slashes($data->appcode));
            foreach ($data as $item) {
                if ($item->data->id == "login") {
                    $password = $item->properties[1]->value;
                    break;
                }
                if ($item->data->id == "wrapper") {
					foreach($item->subitems AS $subitem){
						if ($subitem->data->id == "login") {
							$password = $subitem->properties[1]->value;
							break;
						}
					}
				}
            }
            if (trim($password) == '') {
                return '202';
            } else if (trim($password) == trim($client_password)) {
                return '200';
            }
        } else {
            return '201';
        }
    }

    public function wifipassword() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $id_place = $this->getParamString('id_place');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $client_password = $this->getParamString('password');
        $type_time_session = $this->getParamString('type_time_session');
        $check = $this->wifipassword_check($id_campaign, $client_password);
        if ($check == '200') {
            $landing_page = $this->getParamString('landing_page');
            $this->session->set_userdata(array('landing_page' => $landing_page));
            $this->checktimesesion($type_time_session);
            die($this->Mother->saveWifiPasswordDb($mac, $nasid, $id_campaign, $name_device, $id_place, $client_password));
        } else {
            die($check);
        }
    }

    public function checkfacebooklike() {
        $mac = $this->getParamString('mac');
        $facebook_url = $this->getParamString('facebook_url');
        $id_campaign = $this->getParamString('id_campaign');
        die($this->Mindex->checkFacebooklike($mac, $facebook_url, $id_campaign));
    }

    public function checkemail() {
        $mac = $this->getParamString('mac');
        $id_campaign = $this->getParamString('id_campaign');
        die($this->Mindex->checkEmail($mac, $id_campaign));
    }

    public function checkphone() {
        $mac = $this->getParamString('mac');
        $id_campaign = $this->getParamString('id_campaign');
        die($this->Mindex->checkPhone($mac, $id_campaign));
    }

    public function checkwifipassword() {
        $mac = $this->getParamString('mac');
        $id_campaign = $this->getParamString('id_campaign');
        $data = $this->Mindex->checkLoginWifi($mac, $id_campaign);
        if ($data == '200') {
            echo '200';
        } else {
            $obj = json_decode($data);
            $password = isset($obj[0]->password) ? $obj[0]->password : 0;
            if ($this->wifipassword_check($id_campaign, $password)) {//Kiểm tra password trước đó với password hiện tại
                echo $data;
            } else {
                echo '200';
            }
        }
        die();
    }

    private function checktimesesion($id) {//set user session
        $arr = array(
            '1' => array('username' => 'time1', 'password' => 'hello'),
            '2' => array('username' => 'time2', 'password' => 'hello'),
            '3' => array('username' => 'time3', 'password' => 'hello'),
            '4' => array('username' => 'time4', 'password' => 'hello'),
            '5' => array('username' => 'time5', 'password' => 'hello'),
            '6' => array('username' => 'time6', 'password' => 'hello')
        );
        if ($id <= count($arr)) {
            $this->session->set_userdata(array(
                'modem_user' => $arr[$id]['username'],
                'modem_password' => $arr[$id]['password']
            ));
        } else {
            $this->session->set_userdata(array(
                'modem_user' => 'time6',
                'modem_password' => 'hello'
            ));
        }
    }

    public function checkcoupon() {
        $mac = $this->getParamString('mac');
        $id_campaign = $this->getParamString('id_campaign');
        $type_coupon = $this->getParamString('type_coupon');
        die($this->Mindex->checkCoupon($mac, $id_campaign, $type_coupon));
    }

    public function campaigncoupon() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $id_place = $this->getParamString('id_place');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $type = $this->getParamInt('type');
        $mobile_email = strtolower($this->getParamString('mobile_email'));
        //$code_coupon = $this->getParamString('code_coupon');
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        die($this->Mother->campaignCouponDb($mac, $nasid, $id_campaign, $name_device, $id_place, $mobile_email, $type));
    }

    public function campaignsendgiftcoupon() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $id_place = $this->getParamString('id_place');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        $mobile = strtolower($this->getParamString('mobile'));
        $email = strtolower($this->getParamString('email'));
        $fullname = $this->getParamString('fullname');
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        //$code_coupon = $this->getParamString('code_coupon');
        //die('111');
        die(json_encode($this->Mother->campaignSendGiftCouponDb($mac, $nasid, $id_campaign, $name_device, $id_place, $mobile, $fullname, $email)));
    }

    public function campaigngiftcoupon() {
        $mac = $this->getParamString('mac');
        $nasid = $this->getParamString('nasid');
        $id_place = $this->getParamString('id_place');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $landing_page = $this->getParamString('landing_page');
        $this->session->set_userdata(array('landing_page' => $landing_page));
        //$type = $this->getParamInt('type');
        //$mobile_email = strtolower($this->getParamString('mobile_email'));
        //$code_coupon = $this->getParamString('code_coupon');
        $type_time_session = $this->getParamString('type_time_session');
        $this->checktimesesion($type_time_session);
        die(json_encode($this->Mother->campaignGiftCouponDb($mac, $nasid, $id_campaign, $name_device, $id_place)));
    }

    public function campaigncouponsendsms() {
        die('loading');
        /* $mac = $this->getParamString('mac');
          $nasid = $this->getParamString('nasid');
          $id_place = $this->getParamString('id_place');
          $id_campaign = $this->getParamString('id_campaign');
          $name_device = $this->getParamString('name_device');
          $landing_page = $this->getParamString('landing_page');
          $this->session->set_userdata(array('landing_page' => $landing_page));
          $type = $this->getParamInt('type');
          $mobile_email = $this->getParamString('mobile_email');
          //$code_coupon = $this->getParamString('code_coupon');
          die($this->Mother->campaignCouponDb($mac, $nasid, $id_campaign, $name_device, $id_place, $mobile_email,$type));
         */
    }
	
	public function syncgetcoupon(){
		$code_coupon = $this->getParamString('code_coupon');
		$email = $this->getParamString('email');
		die($this->Mother->syncgetcouponDb($code_coupon, $email));
	}
	
}
